package com.mstarc.app.mnfc.mstarcApiUtils;

import android.util.Log;

import mstarc_os_api.mstarc_os_api_msg;

/**
 * @author 崔健伟
 * @version V1.0
 * @Title: InitSEIDUtil.java
 * @Package com.mstarc.app.mnfc.mstarcApiUtils
 * @Description:
 * @date 2017/6/21 10:44
 */
public class InitSEIDUtil {
    private static final String TAG = "InitSEIDUtil";

    /*请求SEID参数：
        1、int[] apdu_read_seid = new int[] {0x80, 0xCA, 0x00, 0x44, 0x00};
        2、int[] apdu_read_seid = new int[] {0x80, 0xCA, 0x00, 0x44, 0xXX};
        最后的值需要根据第一步返回值进行动态设定
        */
    public static int[] initSEID(mstarc_os_api_msg m_api_msg) {
        MstarcResetUtil.reset(m_api_msg);
        if (SelectUtil.select(m_api_msg)) {
            int[] apdu_read_seid = new int[]{0x80, 0xCA, 0x00, 0x44, 0x00};
            int[] nfc_buf = ApduUtil.apdu(m_api_msg, apdu_read_seid);
            Log.d(TAG, "第一次请求参数:= " + BytetohexStringUtil.BytetohexString(apdu_read_seid));
            Log.d(TAG, "发送返回数据1:="
                    + "===numFor10:=" + nfc_buf[0]
                    + "====numFor16:=" + BytetohexStringUtil.BytetohexString(nfc_buf));
            if (nfc_buf != null) {
                if (nfc_buf[0] == 0x6c) {
                    apdu_read_seid[apdu_read_seid.length - 1] = nfc_buf[1];
                    int[] nfc_buf2 = ApduUtil.apdu(m_api_msg, apdu_read_seid);
                    Log.d(TAG, "第二次请求参数:= " + BytetohexStringUtil.BytetohexString(apdu_read_seid));
                    Log.d(TAG, "发送返回数据2:="
                            + "===numFor10:=" + nfc_buf2[0]
                            + "====numFor16:=" + BytetohexStringUtil.BytetohexString(nfc_buf2));
                    return nfc_buf2;
                } else if (nfc_buf[0] == 0x6a) {
                    Log.d(TAG, "initSEID: 未写入SEID！！！");
                }
            }
        }
        return null;
    }

    /**
     * 将获取的seid进行处理
     *
     * @param nfc_buf
     * @return
     */
    public static int[] getSEID(int[] nfc_buf) {
        int[] seids = new int[nfc_buf.length - 4];
        if (nfc_buf.length > 4) {
            for (int i = 0; i < seids.length; i++) {
                seids[i] = nfc_buf[i + 2];
            }
        }
        return seids;
    }
}
